﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TimKen.DataEntity
{
    public class ShopCartSet : IEnumerable<ShopCartItem>
    {
        private Dictionary<string, ShopCartItem> items;
        public ShopCartSet()
        {
            this.items = new Dictionary<string, ShopCartItem>();
        }
        public Dictionary<string, ShopCartItem> GetItems {
            get { return this.items; }
        }
        /// <summary>
        /// 各项总原价
        /// </summary>
        public decimal TotalPrice
        {
            get
            {
                decimal price = (decimal)0;
                foreach (ShopCartItem item in this)
                {
                    price = price + item.Money;
                }
                return price;
            }
        }
        public bool IsCommit
        {
            get {
                bool bo = true;
                foreach (ShopCartItem item in this)
                {
                    if (item.IsShow == false)
                    {
                        bo = false;
                        break;
                    }
                }
                return bo;
            }
        }
        /// <summary>
        /// 各项税后总价
        /// </summary>
        public decimal SHTotalPrice
        {
            get
            {
                decimal price = (decimal)0;
                foreach (ShopCartItem item in this)
                {
                    price = price + item.ShMoney;
                }
                return price;
            }
        }

        /// <summary>
        /// 总净量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public float ToJl
        {
            get
            {
                float jl = (float)0;
                foreach (ShopCartItem item in this)
                {
                    jl = jl + item.Project.Netweight*item.Count;
                }
                return jl;
            }
        }
        public ShopCartItem this[string id]
        {
            get
            {
                return this.items[id.Trim()];
            }
            set
            {
                this.items[id] = value;
            }
        }
        public void Add(Project product, int count, bool isAuthorizationed)
        {
            this.Add(new ShopCartItem(product, count, isAuthorizationed));
        }
        public void Add(ShopCartItem item)
        {
            if (!this.items.ContainsKey(item.Project.ProjectId))
            {
                this.items.Add(item.Project.ProjectId, item);
            }
            else
            {
                int nu = item.Count == 0 ? 0 : item.Count;
                this.items[item.Project.ProjectId].Count = this.items[item.Project.ProjectId].Count+nu;
            }
        }
        public void Remove(string key)
        {
            this.items.Remove(key);
        }
        public void Remove(Project product)
        {
            this.items.Remove(product.ProjectId);
        }
        public void Remove(ShopCartItem shopCartItem)
        {
            this.items.Remove(shopCartItem.Project.ProjectId);
        }
        public IEnumerator<ShopCartItem> GetEnumerator()
        {
            return this.items.Values.GetEnumerator();
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.items.Values.GetEnumerator();
        }

    }
}
